﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="recDebtorSector.aspx.cs"
    Inherits="lists_Administration_Catalog_recDebtorSector" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/helper.js") %>"></script>
    <br />
    <table id="tblSector" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pgrSector" class="scroll">
    </div>
    <br />
    <button id="btnSave" type="button">
        Simpan</button>
    <button id="btnClose" type="button">
        Tutup</button>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

    </script>
    <script type="text/javascript">

        var DebtorID = '<%= Request.Params["DebtorID"] %>';

       // var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Administration/Catalog/recSubSubSectorhandler.ashx?method=getdata") %>';

        var idsOfSelectedRows = [], //idsOfSaved = [],

        // idsOfSaved = For storing all ids data from DB and hold selected data too.

        updateIdsOfSelectedRows = function (id, isSelected) {

            var SubSubSectorID = $('#tblSector').jqGrid('getCell', id, 'SubSubSectorID');

            var index = $.inArray(SubSubSectorID, idsOfSelectedRows);
           // var idx = $.inArray(SubSubSectorID, idsOfSaved);

            if (!isSelected && index >= 0) {
                idsOfSelectedRows.splice(index, 1); // remove id from the list
            }
            else if (index < 0) {
                idsOfSelectedRows.push(SubSubSectorID);
            }

//            if (!isSelected && idx >= 0) {
//                idsOfSaved.splice(idx, 1);
//            }
//            else if (idx < 0) {
//                if (SubSubSectorID > 0)
//                    idsOfSaved.push(SubSubSectorID);
//            
//            }
        };

        $('#tblSector').jqGrid({
            url: '<%= ResolveUrl("~/AjaxHandler/Administration/Catalog/recSubSubSectorhandler.ashx?method=getdata") %>',
            datatype: 'json',
            colNames: ['Sub sub sektor id', 'Sektor', 'Sub Sektor', 'Sub Sub Sektor'],
            colModel: [
            { name: 'SubSubSectorID', hidden: true, index: 'SubSubSectorID', width: 200 },
           	    { name: 'recSubSector.recSector.SectorName', index: 'SectorName', width: 200 },
                { name: 'recSubSector.SubSectorName', index: 'SubSectorName', width: 200 },
                { name: 'SubSubSectorName', index: 'SubSubSectorName', width: 200 },
            ],
            rowNum: 10,
            id: 'SubSubSectorID',
            multiselect: true,
            rowList: [5, 10, 15, 20],
            pager: '#pgrSector',
            sortname: 'SECTORCODE',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 600,
            height: '100%',
            caption: 'Sektor',
            gridview: true,
            rownumbers: true,
            ajaxGridOptions: { cache: false },
            onSelectRow: updateIdsOfSelectedRows,
            onSelectAll: function (rowsId, isSelected) {

                for (var i = 0; i < rowsId.length; i++) {
                    var id = rowsId[i];
                    updateIdsOfSelectedRows(id, isSelected);
                }

            },
            loadComplete: function (data) {

                $.ajax({
                    type: 'POST',
                    url: '<%= ResolveUrl("~/ajaxhandler/administration/catalog/recdebtorsectorhandler.ashx") %>' + '?method=GetDataByDebtor&debtorId=' + DebtorID,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (rows) {

                        $.each(rows, function (r, row) {

                            for (var i = 0; i < data.rows.length; i++) {
                                if (row.SubSubSectorID == data.rows[i].SubSubSectorID) {
                                    var idx = i + 1;
                                    updateIdsOfSelectedRows(idx, true);
                                }
                            }

//                            var idx = $.inArray(row.SubSectorID, idsOfSaved);

//                            if (idx < 0) {
//                                    idsOfSaved.push(row.SubSectorID);
//                            }

                        });

                        for (var i = 0; i < data.rows.length; i++) {
                            for (var x = 0; x < idsOfSelectedRows.length; x++) {

                                if (data.rows[i].SubSubSectorID == idsOfSelectedRows[x]) {
                                    var idx = i + 1;
                                    $('#tblSector').jqGrid('setSelection', idx);
                                }
                            }
                        }

                    },
                    error: function () {
                        alert("Failed to load Sector");
                    }
                });

            }
        });

        function ClickSave() {

            var SubSubSectorIds = '';
            for (i = 0; i < idsOfSelectedRows.length; i++) {
                if (SubSubSectorIds == '')
                    SubSubSectorIds = idsOfSelectedRows[i];
                else
                    SubSubSectorIds += ',' + idsOfSelectedRows[i];

            }

            var objDebtor = new Object();
            objDebtor.DebtorID = encodeURIComponent(DebtorID);
            objDebtor.SubSubSectorID = encodeURIComponent(SubSubSectorIds);

            jsonData = JSON.stringify(objDebtor);

            CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/catalog/recDebtorSectorhandler.ashx") %>', "?method=updatebydebtor", jsonData);

            parent.$('#dialog').dialog('close');

        }


        $('#btnSave').click(function () {

            ClickSave();
            return false;
        });


        $('#btnClose').click(function () {
            parent.$('#dialog').dialog('close');

            return false;
        });
    
    </script>
    </form>
</body>
</html>
